/**
 * 所有自定义的指令
 * Created by douxc on 2015/4/20.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.directive("topTitle", function () {
      //顶部导航栏
      return {
        restrict: 'E',
        replace: true,
        scope: {
          title: '@',
          icon: '@',
          isBmap: '@'
        },
        templateUrl: 'template_views/top-title.html',
        controller: function ($scope) {
          $scope.showPersonal = function () {
            angular.element('#title-dropdown').dropdown('toggle');
          }
        }
      }
    });
    app.directive("mhFormContent", function () {
      return {
        //登录表单
        restrict: 'E',
        replace: true,
        transclude: true,
        scope: {
          config: '=configData',
          model: '=ngModel'
        },
        templateUrl: 'template_views/form_item_with_icon.html'
      }
    });
    app.directive("panelList", function () {
      //带有标题的列表
      return {
        restrict: 'E',
        replace: true,
        scope: {
          listTitle: '@',
          linkUrl: '@',
          url: '=queryUrl',
          listType: '@'
        },
        templateUrl: 'template_views/panel-list.html',
        controller: function ($scope, MHService, $rootScope) {
          $scope.listType = ($scope.listType === 'second') ? 'second' : 'default';
          var index = 1;
          $rootScope.repeat = undefined;
          MHService.pageQuery({url: $scope.url, pageIndex: index}, function (data) {
            $scope.repeat = data['data'];
            $scope.hasNext = data['hasNext'];
            $rootScope.repeat = $scope.repeat;
          });
          /**
           * 下一页
           */
          $scope.nextPage = function () {
            index++;
            MHService.pageQuery({url: $scope.url, pageIndex: index}, function (data) {
              var temp = angular.copy($scope.repeat);
              $scope.repeat = temp.concat(data['data']);
              $scope.hasNext = data['hasNext'];
              $rootScope.repeat = $scope.repeat;
            });
          };
        }
      }
    });
    app.directive('horizontalItem', function () {
      //水平排列的表单元素
      return {
        restrict: 'E',
        replace: true,
        scope: {
          config: '=configData',
          model: '=ngModel'
        },
        templateUrl: 'template_views/horizontal-item.html'
      }
    });
    app.directive('horizontalItemStatic', function () {
      //水平排列的表单元素
      return {
        restrict: 'E',
        replace: true,
        scope: {
          config: '=configData',
          model: '=ngModel'
        },
        templateUrl: 'template_views/horizontal-item-static.html'
      }
    });
    app.directive('horizontalItemSecond', function () {
      //水平排列的表单元素
      return {
        restrict: 'E',
        replace: true,
        scope: {
          config: '=configData',
          model: '=ngModel'
        },
        templateUrl: 'template_views/horizontal-item-second.html'
      }
    });
    app.directive('horzontalStaticItem', function () {
      return {
        restrict: 'E',
        replace: true,
        scope: {
          title: '@',
          content: '='
        },
        templateUrl: 'template_views/horizontal-static-item.html'
      }
    });
    app.directive('mhSelect', function () {
      return {
        restrict: 'E',
        replace: true,
        scope: {
          title: '@',
          selected: '=',
          options: '=ngModel'
        },
        templateUrl: 'template_views/mh-select.html'
      }
    });
    app.directive('mhTimeLineList', function () {
      return {
        restrict: 'E',
        replace: true,
        controller: function ($timeout, $scope, MHService) {
          var cIndex = 1, sIndex = 1, cMessages = null, sMessages = null;//公司消息分页，系统消息分页;公司消息、系统消息
          var messageType = 1;//类型：公司消息
          /**
           * 查询公司消息数据
           * TODO 消息类型目前没有使用到，全部是0，测试数据
           */
          function queryCMessage(pIndex) {
            MHService.pageQuery({url: MHService.URL.messages + 0, pageIndex: pIndex}, function (data) {
              MHService.messageFormat(data).then(function (result) {
                if (pIndex === 1) {
                  //第一次进入
                  cMessages = result['data'];
                } else {
                  cMessages['data'] = cMessages['data'].concat(result['data']);
                }
                $scope.messages = cMessages;
                $scope.hasMore = result['hasNext'];
                //计算时间线高度
                $timeout(function () {
                  var listh = angular.element('#messageList').height();
                  angular.element('#timeLine').height(listh - 12);
                });
              });
            });
          }

          queryCMessage(cIndex);
          /**
           * 查询系统消息 1222
           * @param sIndex
           */
          function querySMessage(sIndex) {
            MHService.pageQuery({url: MHService.URL.messages + '-2', pageIndex: sIndex}, function (data) {
              MHService.messageFormat(data).then(function (result) {
                if (sIndex === 1) {
                  //第一次进入
                  sMessages = result['data'];
                } else {
                  sMessages['data'] = sMessages['data'].concat(result['data']);
                }
                $scope.messages = sMessages;
                $scope.hasMore = result['hasNext'];
                //计算时间线高度
                $timeout(function () {
                  var listh = angular.element('#messageList').height();
                  angular.element('#timeLine').height(listh - 12);
                });
              });
            });
          }

          /**
           * 查看不同类型的消息
           * @param type
           */
          $scope.changeMsgType = function (type) {
            messageType = type;
            if (type === 1) {
              //公司消息，且缓存中有数据
              if (null !== cMessages) {
                $scope.messages = cMessages;
              } else {
                queryCMessage(cIndex);
              }
            } else {
              //系统消息，且缓存中有数据
              if (null !== cMessages) {
                $scope.messages = sMessages;
              } else {
                querySMessage(sIndex);
              }
            }
          };
          /**
           * 加载更多
           */
          $scope.loadMore = function () {
            if (messageType === 1) {
              //公司消息
              ++cIndex;
              queryCMessage(cIndex);
            } else {
              //系统消息
              ++sIndex;
              querySMessage(sIndex);
            }
          }
        },
        templateUrl: 'template_views/time-line-list.html'
      }
    });
  };
});
